#include "faster.h"

bool insertFile(MYSQL *mysql,char *filename)
{
    char q[1000];
    int flag,r;
    time_t p;

    p = time((time_t*)NULL);

    r = sprintf(q,"insert into log_file(filename,doTime)values('%s',%d)",filename,p);

    if (r>0)
    {
        flag = mysql_real_query(mysql,q,(unsigned int)r);
        return flag == 0;
    }

    return false;
}

int fileExist(MYSQL *mysql,char *filename)
{
    char q[1000];
    int flag,r;
    MYSQL_RES *res;
    MYSQL_ROW row;

    r = sprintf(q,"Select count(*) from log_file where filename='%s'",filename);
    if (r>0)
    {
        flag = mysql_real_query(mysql,q,(unsigned int)r);
        if(!flag)
        {
            res =  mysql_store_result(mysql);
            row = mysql_fetch_row(res);
            return atoi(row[0]);
        }
    }
    return 0;
}

int insertLog(MYSQL *mysql,const char *filename,const char *log)
{

    int flag,r;
    char q[1000],t[1000],f[120];
    mysql_escape_string(t,log,strlen(log));
    mysql_escape_string(f,filename,strlen(filename));
    r = sprintf(q,"insert into log_log(filename,log)values('%s','%s')",f,t);

    if (r>0)
    {
        flag = mysql_real_query(mysql,q,(unsigned int)r);
        return flag == 0 ? 1:0;
    }

    return 0;
}
